;(function($){
	$.widget("tempo.decisiontree",{
		options:{
			descisionTreeStr:"",
			distype:"",
		},
		_create:function(){
			// 解析决策树并构建html字符串
			var count1=0;
			var parseJsonTree = function(jsonTree, bRoot, layerNum) {
				if (jsonTree && jsonTree.length > 0) {
					try {
						for (var i = 0; i < jsonTree.length; i++) {
							var itemObject = jsonTree[i];
							if (bRoot) {
								layerNum = 1;
							}
							dtStr += "<div><span class='layer_" + layerNum + "'>"+ itemObject["name"] +"</span><ul>";
							count1++;
							if (itemObject["list"] != null) {
								layerNum++;
								parseJsonTree(itemObject["list"], false, layerNum);
								layerNum--;
							}
							dtStr += "</ul></div>";
						}
					} catch(e) {
						
					}
					return dtStr;
				}
			}
			
			//解析决策树（仅显示前200行信息）
			var count=0;
			var parseLimitJsonTree = function(jsonTree, bRoot, layerNum) {
				if (jsonTree && jsonTree.length > 0) {
					try {
						for (var i = 0; i < jsonTree.length; i++) {
							var itemObject = jsonTree[i];
							if (bRoot) {
								layerNum = 1;
							} 
							if(count==200){
								dtStr+='<div><span>...</span></div><div id="downloadall-decisiontree" class="downloadall-decisiontree"><span>'+TempoUtils.locale("mine.download_all_tip")+'</span>';
								count++;
								return dtStr;
							}
							if(count>200){
								return dtStr;
							}
							dtStr += "<div><span class='layer_" + layerNum + "'>"+ itemObject["name"] +"</span><ul>";
							count++;
							
							if (itemObject["list"] != null) {
								layerNum++;
								parseLimitJsonTree(itemObject["list"], false, layerNum);
								layerNum--;
							}
							dtStr += "</ul></div>";
						}
					} catch(e) {
						
					}
					return dtStr;
				}
			}
			
			var that  = this.element;
			var descisionTreeStr = this.options.descisionTreeStr;
			var treeModelArr = [];
			var jsonTree;
			try{
				jsonTree = JSON.parse(descisionTreeStr);
			}catch(e){
				console.log("history flow")
				return false
			}
			
			this._getTreeModelData(jsonTree, treeModelArr);
			var dicisionTreeDiv = $("<div/>").addClass("mine-dicisiontree-div").appendTo(that);
			TempoMineUtils.getDownLoadBtn().css({"margin-left":"0px","margin-top":"5px"}).unbind("click").bind("click",function(){
				var treemodeldata = {
						"treemodeldata":TempoMineUtils.encodeHTML(treeModelArr.join("\n")),
						"fileName":"treemodel.txt"
				};
				TempoUtils.downLoadFile({
					url: contextPath + "/api/ai/mineresource/downloadTreeModel",
					data :treemodeldata
				});
			}).appendTo(dicisionTreeDiv);
			
			$("<div/>").addClass("mine-decisiontree-btn").appendTo(dicisionTreeDiv).append($('<input type="button" id="btn_open" value="'+TempoUtils.locale("mine.zhankai")+'"/>')).append($('<input type="button" id="btn_close" value="'+TempoUtils.locale("mine.zhedie")+'"/>'));
			var dtContainer = $("<div id='mine-decisiontree' class='mine-decisiontree'>").appendTo(dicisionTreeDiv);
			var dtPathTableContainer = $("<div/>").addClass("mine-dicisiontree-path-table").appendTo(that);
			var distype=this.options.distype;
			var dtStr = "";
			var jsonTreeStr = "";
			if(distype=="all"){
				jsonTreeStr = parseJsonTree(jsonTree, true, 1);
			}else if(distype=="limit"){
				jsonTreeStr = parseLimitJsonTree(jsonTree, true, 1);
			}else{
				jsonTreeStr = parseLimitJsonTree(jsonTree, true, 1);
			}
			console.log("count1:"+count1);
			console.log("count:"+count);
			dtContainer.html(jsonTreeStr);
			this._createDescisionTree();
			if(distype=="all"){
				that.find("#mine-decisiontree ul").show(300);
				this._openOrCloseTree("−");
			
			}
			this._bindEvent();
			var metas = [["IntegerType", "StringType"]];
			var metaNames = [TempoUtils.locale("mine.cengji"),TempoUtils.locale("mine.tiaojian")];
			var tableInfo = TempoMineUtils.buildTableByMeta(metas, metaNames,"tempo-mine-insight-table","");
			that.find('.mine-dicisiontree-path-table').empty();
			that.find('.mine-dicisiontree-path-table').append(tableInfo);
		},
		_getBlankStr:function(level) {
			var blankStr = "";
			for (var i = 0; i < level; i++) {
				blankStr += "\t";
			}
			return blankStr;
		},
		_getTreeModelData:function(jsonTreeArr, treeModelArr) {
			for (var i = 0; i < jsonTreeArr.length; i++) {
				var jsonTree = jsonTreeArr[i];
				var blankStr = this._getBlankStr(jsonTree.level);
				treeModelArr.push(blankStr+jsonTree.name);
				if (jsonTree.list) {
					this._getTreeModelData(jsonTree.list, treeModelArr);
				}
			}
		},
		_createDescisionTree:function(){
			var that = this.element;
			that.find("#mine-decisiontree ul").each(function(idx, ele){
				var ulContent = $(ele).text();
				if (ulContent) {
					var spanContent = $(ele).siblings("span").text();
					$(ele).siblings("span").text("[+] " + spanContent);
				}
			});
		},
		_bindEvent:function() {
			var that = this.element;
			var $this = this;
			that.find("#mine-decisiontree").find("div span").click(function(){
				var ul = $(this).siblings("ul");
				var className = $(this).prop("className");
				var spanStr = $(this).text();
				var spanContent = spanStr;
				if (spanStr.indexOf("[+]") != -1 || spanStr.indexOf("[−]") != -1) {
					spanContent = spanStr.substr(3, spanStr.length);
					if (ul.find("div").text() != null) {
						if (ul.css("display") == "none") {
							ul.show(300);
							if (TempoMineUtils.startsWith(spanContent," ")) {
								$(this).text("[−]" + spanContent);
							} else {
								$(this).text("[−] " + spanContent);
							}
						} else {
							ul.hide(300);
							if (TempoMineUtils.startsWith(spanContent," ")) {
								$(this).text("[+]" + spanContent);
							} else {
								$(this).text("[+] " + spanContent);
							}
						}
					}
				}
				var layerNum = parseInt(className.split("_")[1]);
				var layerStrs = [spanContent];
				var curSpanObj = $(this);
				for (var i = layerNum - 1; i >= 1;--i) {
					var parent = curSpanObj.parent().parent().parent();
					var curSpanObj = parent.find(".layer_" + i);
					var curSpanText = curSpanObj.text();
					if (curSpanText.indexOf("[+]") != -1 || curSpanText.indexOf("[−]") != -1) {
						curSpanText = curSpanText.substr(4,curSpanText.length);
					}
					layerStrs.push(curSpanText);
				}
				var metas = [["IntegerType", "StringType"]];
				var metaNames = [TempoUtils.locale("mine.cengji"),TempoUtils.locale("mine.tiaojian")];
				for (var i = layerStrs.length-2; i >= 0; i--) {
					metas.push([layerStrs.length - i - 1,layerStrs[i]]);
				}
				var tableInfo = TempoMineUtils.buildTableByMeta(metas, metaNames,"tempo-mine-insight-table","");
				that.find('.mine-dicisiontree-path-table').empty();
				that.find('.mine-dicisiontree-path-table').append(tableInfo);
			});
			
			that.find("#btn_open").button({
				"click":function(){
					var process = that.tempoprogressbar();
					setTimeout(function(){
						that.find("#mine-decisiontree ul").show(300);
						$this._openOrCloseTree("−");
//						第一次加载数据，切换show时滚动条状态并不会刷新，手动切换滚动条效果，使得浏览器重新渲染滚动条效果
						that.find("#mine-decisiontree ul").css("overflow","hidden");
						that.find("#mine-decisiontree ul").css("overflow","visible");
						process.tempoprogressbar("destroy");
					},500);
			}});
			
			that.find("#btn_close").button({
				"click":function(){
					var process = that.tempoprogressbar();
					setTimeout(function(){
						that.find("#mine-decisiontree ul").hide(200);
						$this._openOrCloseTree("+");
						
						var metas = [["IntegerType", "StringType"]];
						var metaNames = [TempoUtils.locale("mine.cengji"),TempoUtils.locale("mine.tiaojian")];
						var tableInfo = TempoMineUtils.buildTableByMeta(metas, metaNames,"tempo-mine-insight-table","");
						that.find('.mine-dicisiontree-path-table').empty();
						that.find('.mine-dicisiontree-path-table').append(tableInfo);
						process.tempoprogressbar("destroy");
					},500);
			}});
		},
		_openOrCloseTree:function(val) {
			
			var that = this.element;
			
			that.find("#mine-decisiontree span").each(function(idx, ele){
				var ul = $(this).siblings("ul");
				var spanStr = $(this).text();
				var spanContent = spanStr;
				if (spanStr.indexOf("[+]") != -1 || spanStr.indexOf("[−]") != -1) {
					spanContent = spanStr.substr(4, spanStr.length);
				}
				if (ul.find("div").html() != null) {
					$(this).text("[" + val + "] " + spanContent);
				}
			}
			
			
			);
			
		},
		_destroy:function(){
			this._super(); 
		}
	});
})(jQuery)